import Vue from 'vue'
import Router from 'vue-router'
import {  getStorage,isPhone,setStorage ,deleteStorage,decrypt} from "UTILS/util";
import { CONSENT_GET_USER} from "_SERVICES/api";
import { Message } from 'element-ui';

Vue.use(Router)

var router = new Router({
  routes: [

    {
      path: "*",
      redirect: "/login",
      meta: {
        title: "平台科室知情同意书系统登录"
      }
    },
    {
      path: '/login',
      name: 'login',
      component: resolve => require(['_VIEW/login/index'], resolve),
      meta: {
        title: "平台科室知情同意书系统登录"
      }
    },
    {
      path: '/notFound',
      name: 'notFound',
      component: resolve => require(['_VIEW/404/index'], resolve),
      meta: {
        title: "404"
      }
    },
    {
      path: '/mainHtml',
      name: 'mainHtml',
      component: resolve => require(['_VIEW/main/index'], resolve),
      meta: {
        title: "平台科室知情同意书系统",
      },
      children:[{
        path: '/department',
        name: 'department',
        component: { render: h => h('router-view') },
        meta: {
          title: "科室",
        },
        redirect:{name:'nj_list'},
        children: [{
          path: '/nj_list',
          name: 'nj_list',
          component: resolve => require(['_VIEW/nj_list/index'], resolve),
          meta: {
            title: "内镜中心",
            ID:"1_1",
          },
        },{
          path: '/hyx_list',
          name: 'hyx_list',
          component: resolve => require(['_VIEW/hyx_list/index'], resolve),
          meta: {
            title: "核医学科",
            ID:"2_1",
          },
        },{
          path: '/kf_list',
          name: 'kf_list',
          component: resolve => require(['_VIEW/kf_list/index'], resolve),
          meta: {
            title: "康复科",
            ID:"3_1",
          },
        },{
          path: '/tj_list',
          name: 'tj_list',
          component: resolve => require(['_VIEW/tj_list/index'], resolve),
          meta: {
            title: "体检中心",
            ID:"4_1",
          },
        },{
          path: '/fsk_list',
          name: 'fsk_list',
          component: resolve => require(['_VIEW/fsk_list/index'], resolve),
          meta: {
            title: "放射科",
            ID:"5_1",
          },
        },{
          path: '/mzmz_list',
          name: 'mzmz_list',
          component: resolve => require(['_VIEW/mzmz_list/index'], resolve),
          meta: {
            title: "麻醉评估门诊",
            ID:"6_1",
          },
        },{
          path: '/fklc_list',
          name: 'fklc_list',
          component: resolve => require(['_VIEW/fklc_list/index'], resolve),
          meta: {
            title: "妇科治疗医嘱列表",
            ID:"7_1",
          },
        },{
          path: '/njsqd_list',
          name: 'njsqd_list',
          component: resolve => require(['_VIEW/njsqd_list/index'], resolve),
          meta: {
            title: "内镜申请单列表",
            ID:"8_1",
          },
        },{
          path: '/xdsls_list',
          name: 'xdsls_list',
          component: resolve => require(['_VIEW/xdsls_list/index'], resolve),
          meta: {
            title: "心电生理室",
            ID:"9_1",
          },
        },{
          path: '/ebhk_list',
          name: 'ebhk_list',
          component: resolve => require(['_VIEW/ebhk_list/index'], resolve),
          meta: {
            title: "耳鼻喉科检查",
            ID:"10_1",
          },
        }]
      },{
        path: '/template',
        name: 'template',
        component: { render: h => h('router-view') },
        meta: {
          title: "模板",
        },
        children: [{
          path: '/templateList',
          name: 'templateList',
          component: resolve => require(['_VIEW/templateList/index'], resolve),
          meta: {
            title: "同意书模板列表",
            ID:"1_2",
          },
        },{
          path: '/consentList',
          name: 'consentList',
          component: resolve => require(['_VIEW/consentList/index'], resolve),
          meta: {
            title: "已签知情同意书列表",
            ID:"1_3",
          },
        },{
          path: '/setTemplate',
          name: 'setTemplate',
          component: resolve => require(['_VIEW/setTemplate/index'], resolve),
          meta: {
            title: "添加知情同意书模板",
            ID:"1_4",
          },
        }]
      },{
        path: '/management',
        name: 'management',
        component: { render: h => h('router-view') },
        meta: {
          title: "管理",
        },
        children: [{
                     path: '/user',
                     name: 'user',
                     component: resolve => require(['_VIEW/user/index'], resolve),
                     meta: {
                       title: "用户管理",
                       ID:"1_5",
                     },
                   },{
                     path: '/role',
                     name: 'role',
                     component: resolve => require(['_VIEW/role/index'], resolve),
                     meta: {
                       title: "角色管理",
                       ID:"1_6",
                     },
                   },
                   {
                     path: '/edit_list',
                     name: 'edit_list',
                     component: resolve => require(['_VIEW/edit_list/index'], resolve),
                     meta: {
                       title: "公众号信息编辑",
                       ID:"1_7",
                     },

                   },

        ]
      }]
    },
    {
      path: '/consentTemplate',
      name: 'consentTemplate',
      component: resolve => require(['_VIEW/consentTemplate/index'], resolve),
      meta: {
        title: "知情同意书签署",
        ID:"0",
        ZKID:[]
      }
    },
    {
      path: '/consentInfo',
      name: 'consentInfo',
      component: resolve => require(['_VIEW/consentInfo/index'], resolve),
      meta: {
        title: "知情同意书",
        ID:"0",
        ZKID:[]
      }
    },
  ]
})

function getUserInfo(queryInfo,callback){
  if(queryInfo.OUT_YHID){//OUT_YHID 从其他系统跳转过来都要带这个OUT_YHID
    CONSENT_GET_USER({
      YHID: queryInfo.OUT_YHID
    }).then((res)=>{
      if(res.code==0){
        console.log("router user",res)
        if(!res.data.YMQX) {
          res.data.YMQX = [];
        }
        if(!res.data.ROLE_ID) {
          res.data.ROLE_ID = [];
        }
        localStorage.setItem(
          "cst_userInfo",
          JSON.stringify(res.data)
        );
        callback()
      }else{
        deleteStorage("cst_userInfo");
        Message({
          showClose: true,
          message: "用户信息获取失败，无访问权限",
          type: 'error',
          duration: 5 * 1000
        })
        callback()
      }
    })

  }else{
    callback()
  }
}

//1、WHITE_ROUTE 无需传入用户信息即可访问
//2、ZKID:[] 表示只要登录的用户，都可以进行访问的页面
//3、ZKID:[1,2] 限制了只有部分专科的用户登录可以进行查看的页面

// 请先做其中的一种：内镜中心知情同意书-内镜下消化道息肉切除、ESD、EMR知情同意书(1)

var WHITE_ROUTE = ['/login','/notFound']; //无需传入用户信息即可访问

router.beforeEach((to, from, next) => {
  getUserInfo(to.query,function(){
    // //本地信息要注意是否可以使用
    let userInfo = getStorage("cst_userInfo");
    userInfo = userInfo?JSON.parse(userInfo):{};

    if (WHITE_ROUTE.indexOf(to.path) > -1) {
      document.title = to.meta.title;
      next();
      return false;
    }

    if (!userInfo.YHID ) {
      document.title = to.meta.title;
      next({
        path: "/login"
      });
    } else {
      if(to.meta.ID==0) { //如果ID为0 表示所有登录用户都可以访问
        document.title = to.meta.title;
        next();
      }else{
        if(userInfo.YMQX.includes(to.meta.ID) || userInfo.ROLE_ID.includes("1") ||  userInfo.ROLE_ID.includes(1)){//存在本页面的查看权限  ROLE_ID包含'1'表示管理员 都能看
          document.title = to.meta.title;
          next();
        }else{ //不能查看 跳转404
          document.title = to.meta.title;
          next({
            path: "/notFound"
          });
        }
      }
  
    }
  })
})

router.afterEach(route => {

})

export default router
